package templar

class MenuTagLib {
	def menu = { attrs, body ->
		def root = attrs.root;
		if(!(root && root instanceof templar.Menu )){
			throwTagError("Tag [menu] requires the attribute [root]")
		}
		outMenu(root);
	}

	private outMenu(def root){
		out << "<ul>"
		root.children.each{ menuItem ->
			out << "<li>"
			if(menuItem.controller){
				out << "<a "
				if(menuItem.tooltip) {
					out << " title=\"${menuItem.tooltip?.encodeAsHTML()}\""
				}
				out << " href=\"${createLink(controller:menuItem.controller,action:menuItem.action).encodeAsHTML()}\""
				out << ">"
			}
			out << "<span>${menuItem.name.encodeAsHTML()}</span>"
			if(menuItem.controller){
				out << "</a>"
			}
			out << "</li>"
			if(menuItem.children){
				outMenu(menuItem);
			}
		}
		out << "</ul>"
	}
}
